package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao_lzl;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService_lzl;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService_lzl.class)
@Transactional
public class MemberServiceImpl_lzl implements MemberService_lzl {
    @Autowired
    private MemberDao_lzl memberDao_lzl;


    /*
    * 根据手机，查询是否是会员
    * */
    @Override
    public Member findMembertBytelephone(String telephone) {
        return memberDao_lzl.findMembertBytelephone(telephone);
    }
    /*
    * 添加会员
    * */
    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if(password!=null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao_lzl.addMember(member);
    }

    /*
    * 获取会员报表数据
    * */
    @Override
    public Map<String,Object> getMemberReport() {
       //根据要求，求出前一年既是前12个月分的折现图
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.MONTH,-12);
        List<String> months = new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();
        //遍历12个月
        for (int i = 0; i < 12; i++) {
            //从前12个月开始每次+1个月
            calendar.add(calendar.MONTH,1);
            Date date = calendar.getTime();
            //格式化日期
            String format = new SimpleDateFormat("yyyy-MM").format(date);
            //添加到集合
            months.add(format);
            //查询当月的会员数
            String s = format + ".31";
            Integer count = memberDao_lzl.findMemberCountByRegTime(s);
            memberCount.add(count);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("memberCount",memberCount);
        map.put("months",months);
        return map;
    }

    /*
    * 修改会员
    * */
    @Override
    public void submitUpdate(Member member) {
        memberDao_lzl.submitUpdate(member);
    }
}
